<template>
  <div>
    <div>
      <el-form style="text-align: left" :model="searchMsg" status-icon :inline="true" ref="searchFrom"
               label-width="100px" class="demo-form-inline">
        <el-form-item label="ip地址" prop="loginip">
          <el-input v-model="searchMsg.loginip"></el-input>
        </el-form-item>
        <el-form-item label="登录用户名" prop="loginname">
          <el-input v-model="searchMsg.loginname"></el-input>
        </el-form-item>
        <el-form-item label="时间段" prop="logintime">
          <el-date-picker
              v-model="searchMsg.logintime"
              type="datetimerange"
              format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <br>
        <el-form-item style="margin-left:  500px">
          <el-button type="primary" @click="search()">搜索</el-button>
          <el-button @click="restSearch()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="text-align: left">
      <el-button type="danger" @click="removeSelect()">删除选中</el-button>
    </div>
    <el-table
        ref="multipleTable"
        :data="logMsgs"
        tooltip-effect="dark"
        style="width: 100%;text-align: center"
        :header-cell-style="{ textAlign: 'center' }"
        :cell-style="{ textAlign: 'center' }"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="id"
          label="日志id"
          >
      </el-table-column>
      <el-table-column
          prop="loginname"
          label="登录账号"
      >
      </el-table-column>
      <el-table-column
          prop="loginip"
          label="登录ip"
      >
      </el-table-column>
      <el-table-column
          prop="logintime"
          label="登录时间"
      >
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
      >
        <template slot-scope="scope">
          <el-button @click="removeOne(scope.row.id)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[3,5,10,20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>

export default {
  name: "usermenu",
  data() {
    return {
      // 角色数据
      logMsgs: [],
      // 选择框数据
      multipleSelection: [],
      // 分页数据
      pageIndex: 1,
      pageSize: 5,
      total: 0,
      // 权限修改的标记
      permyFlag: false,

      // 搜索的条件
      searchMsg: {},
      // 表单校验

    };
  },
  // 页面初始化方法
  created() {
    this.flushPage()
  },
  methods: {
    // 复选框的方法
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 分页的方法，当页面大小改变
    handleSizeChange(val) {
      this.pageSize = val
      this.flushPage()
    },
    // 当页面下标改变
    handleCurrentChange(val) {
      this.pageIndex = val
      this.flushPage()
    },
    // 删除这条记录
    removeOne(id) {
      let ids = []
      ids.push(id)
      this.removeBeach(ids)
    },
    // 删除选中
    removeSelect() {
      let ids = []
      this.multipleSelection.forEach(row => {
        ids.push(row.id)
      })
      this.removeBeach(ids)
    },
    // 批量删除记录
    removeBeach(ids) {
      const h = this.$createElement
      // 发送请求
      this.$http.post("http://127.0.0.1:8081/Log/Remove", ids).then(resp => {
        if (resp.data.code === 2000) {
          // 消息通知
          this.$notify({
            title: "消息",
            message: h("i", {style: "color: teal"}, resp.data.msg),
            duration: 4000
          })
          // 刷新页面
          this.flushPage()
        } else {
          // 消息通知
          this.$notify.error({
            title: "错误！",
            message: h("i", {style: "color: red"}, resp.data.msg),
            duration: 4000
          })
        }
      })
    },

    // 重置搜索菜单
    restSearch() {
      this.$nextTick(() => {
        this.$refs['searchFrom'].resetFields();
      })
    },
    // 执行搜索方法
    search() {
      this.$http.post("http://127.0.0.1:8081/Log/fuzzySearch", this.searchMsg).then(resp => {
        this.logMsgs = resp.data.data
      })
    },
    // 刷新页面的方法
    flushPage() {
      this.$http.get("http://127.0.0.1:8081/Log/List/" + this.pageIndex + "/" + this.pageSize).then(resp => {
        // 放入菜单信息
        this.logMsgs = resp.data.data.records;
        // 放入分页数据
        this.total = resp.data.data.total
      })
    },

  }
}

</script>

<style scoped>

</style>
